package com.hulk.ratel.config;

import com.baomidou.mybatisplus.enums.DBType;
import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.plugins.SqlExplainInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * @author cmt
 *
 */
@Configuration
@MapperScan(basePackages = { "com.hulk.ratel.persistence.mapper" })
public class MybatisPlusConfig {


	/*
    * 分页插件，自动识别数据库类型
    * 多租户，请参考官网【插件扩展】
    */
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor page = new PaginationInterceptor();
		page.setDialectType(DBType.MYSQL.getDb());

		return page;
	}

	/**
	 * mybatis-plus乐观锁插件
	 * 
	 */
	@Bean
	public OptimisticLockerInterceptor optimisticLockerInterceptor() {
		OptimisticLockerInterceptor lock = new OptimisticLockerInterceptor();
		return lock;
	}


    @Bean
    @Profile({"local","test"})// 设置 dev test 环境开启
	public PerformanceInterceptor performanceInterceptor() {
	    PerformanceInterceptor page = new PerformanceInterceptor();
		page.setMaxTime(100);
		page.setFormat(true);
		return page;
	}


    //SQL 执行分析拦截器【 目前只支持 MYSQL-5.6.3 以上版本 】，作用是分析 处理 DELETE UPDATE 语句， 防止小白或者恶意 delete update 全表操作！
	@Bean
    @Profile({"local","test"})// 设置 dev test 环境开启
	public SqlExplainInterceptor sqlExplainInterceptor() {
		SqlExplainInterceptor sql = new SqlExplainInterceptor();
		sql.setStopProceed(false);
		return sql;
	}



   /*
    * oracle数据库配置JdbcTypeForNull
    * 参考：https://gitee.com/baomidou/mybatisplus-boot-starter/issues/IHS8X
    不需要这样配置了，参考 yml:
    mybatis-plus:
      confuguration
        dbc-type-for-null: 'null'
   @Bean
   public ConfigurationCustomizer configurationCustomizer(){
       return new MybatisPlusCustomizers();
   }

   class MybatisPlusCustomizers implements ConfigurationCustomizer {

       @Override
       public void customize(org.apache.ibatis.session.Configuration configuration) {
           configuration.setJdbcTypeForNull(JdbcType.NULL);
       }
   }
   */

	

}
